﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using SoonCode.Core;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;
using MessageBox = HandyControl.Controls.MessageBox;

namespace SoonCode.FocusTest.ViewModels
{
    internal class TeacherWindowViewModel : BindableBase
    {
        public string memberString { get; set; }
        private IEventAggregator aggregator;
        private int testItemIndex = 0;
        
        public ICommand StepCommand { get; set; }
        public string MemberString
        {
            get
            {
                return memberString;
            }
            set
            {
                memberString = value;
                RaisePropertyChanged();
            }
        }
        public int TestItemIndex
        {
            get { return testItemIndex; }
            set
            {
                testItemIndex = value;
                RaisePropertyChanged();
            }
        }
        public TeacherWindowViewModel(IEventAggregator aggregator)
        {
            StartTest();
            this.aggregator = aggregator;
            this.aggregator.GetEvent<ChangeTestEvent>().Subscribe(NextTest);
            this.aggregator.GetEvent<StartTestEvent>().Subscribe(StartTest);
            this.aggregator.GetEvent<SubmitTestResultEvent>().Subscribe(OnSubmitTest);
            StepCommand = new DelegateCommand<string>(OnStepCommand);
        }

        private void StartTest()
        {
            MemberInfo member = GlobalModel.Instance.MemberInfo;
            MemberString = member.name + "/" + member.sex + "/" + member.showAge;
        }


        private void OnStepCommand(string p)
        {
            if (TestItemIndex == p.ToInt()) return;
            SoundPlayer.Stop();
            this.aggregator.GetEvent<ChangeTestEvent>().Publish(p.ToInt());
        }
        public ICommand OnSpaceKeyPress
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.aggregator.GetEvent<SpaceKeyPressEvent>().Publish();
                });
            }
        }

        public void OnSubmitTest()
        {
            FileHelper.DeleteFile(GlobalModel.AccountDir+ "test.data");
            FileHelper.DeleteFile(GlobalModel.AccountDir + "member.data");

        }
        public void NextTest(int p)
        {
            this.TestItemIndex = p;
            GlobalModel.Instance.MemberInfo.testItem = p;
            string testJson = JsonSerializer.Serialize(GlobalModel.Instance.TestInfo);
            string memberJson = JsonSerializer.Serialize(GlobalModel.Instance.MemberInfo);
            FileHelper.WriteTxt(testJson, GlobalModel.AccountDir + "test.data");
            FileHelper.WriteTxt(memberJson, GlobalModel.AccountDir + "member.data");
        }

    }
}
